﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Reflection;
using System.Utility.Extensions.Attributes;

namespace System.Utility.DBMS
{
    public static class DBHelper
    {
        public static void Delete(string proc, object[] data)
        {
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["DBConnString"], proc, data);

        }

        public static void Update(string proc, object[] data)
        {
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["DBConnString"], proc, data);
        }

        public static void Add(string proc, object[] data)
        {
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["DBConnString"], proc, data);


            //throw new Exception("AE-EU1000");
        }


        public static DataSet GetDataset(string proc, object[] data)
        {
            return SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["DBConnString"], proc, data);
        }

        public static void PopulateObject(object t, DataRow row)
        {
            Type ty = t.GetType();
            foreach (PropertyInfo info in ty.GetProperties())
            {
                try
                {
                    object[] arr = info.GetCustomAttributes(typeof(IncludeAttribute), true);
                    if (arr != null && arr.Length > 0)
                    {
                        IncludeAttribute att = (IncludeAttribute)arr[0];
                        if (!att.Yes)
                            continue;
                    }

                    object val = row[info.Name];
                    info.SetValue(t, val, null);
                }
                catch (Exception ex)
                {
                    info.SetValue(t, "", null);
                }
            }
        }

        public static object[] GetDataByIndex(GridViewRow row, int[] indexes)
        {
            object[] dataArray = new object[indexes.Length + 1];
            int count = dataArray.Length;
            int counter = 0;
            foreach (int cnt in indexes)
            {
                dataArray[counter] = ((TextBox)(row.Cells[cnt].Controls[0])).Text;
                counter++;
            }
            return dataArray;
        }
        public static void ExcuteProc(string proc, object[] data)
        {
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["DBConnString"], proc, data);
        }

        public static DataSet GetDataTable(string proc, object[] data)
        {
            return SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["DBConnString"], proc, data);
        }
    }
}
